//获取url中的参数
function getUrlParam(name) {
    var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
    var r = window.location.search.substr(1).match(reg);  //获取url中"?"符后的字符串并正则匹配
    var context = "";
    if (r != null)
        context = r[2];
    reg = null;
    r = null;
    return context == null || context == "" || context == "undefined" ? "" : context;
}

//获取url中所有参数
function GetRequest() {
    var url = location.search; //获取url中"?"符后的字串
    var theRequest = new Object();
    if (url.indexOf("?") != -1) {
        var str = url.substr(1);
        strs = str.split("&");
        for(var i = 0; i < strs.length; i ++) {
            theRequest[strs[i].split("=")[0]]=unescape(strs[i].split("=")[1]);
        }
    }
    return theRequest;
}

//验证是否微信登录
function isWx() {
    if(navigator.userAgent.toLowerCase().indexOf("micromessenger") == -1) {
        weui.topTips('请使用微信打开网页',{
            duration: 5000,
            callback: function () {
                window.close();
            }
        });
        return false;
    } else {
        return true;
    }
}

//检测userid
function checkUserid() {
    var userid = localStorage.getItem('userid');
    if (!userid) {
        var code = getUrlParam('code');
        if (code) {
            $.ajax({
                url: '/user/id/' + code,
                type: 'get',
                dataType: 'text',
                success: function (data) {
                    localStorage.setItem('userid', data);

                },
                error: function () {
                    weui.topTips('初始化参数失败');
                }
            });
        } else {
            window.location.href = 'https://open.weixin.qq.com/connect/oauth2/authorize' +
                '?appid=' + getUrlParam("appid") +
                '&redirect_uri=' + encodeURI(window.location.href) +
                '&response_type=code&scope=snsapi_base#wechat_redirect';
        }
    }
}

function jssdkConfig(apiList) {
    var href = location.href.split("#")[0];
    console.log(href);
    $.ajax({
        url: "/jsConfig?url=" + encodeURIComponent(href),
        type: "get",
        dataType: "json",
        async: false,
        success: function (data) {
            if(data.errCode == 0){
                var jssdk = JSON.parse(data.result);
                console.log(jssdk);
                wx.config({
                    beta: true,// 必须这么写，否则wx.invoke调用形式的jsapi会有问题
                    debug: false, // 开启调试模式,调用的所有api的返回值会在客户端alert出来，若要查看传入的参数，可以在pc端打开，参数信息会通过log打出，仅在pc端时才会打印。
                    appId: jssdk.corpId, // 必填，公众号的唯一标识
                    timestamp: jssdk.timeStamp, // 必填，生成签名的时间戳
                    nonceStr: jssdk.nonceStr, // 必填，生成签名的随机串
                    signature: jssdk.signature, // 必填，签名，见附录1
                    jsApiList: apiList
                });
            } else {
                weui.topTips("微信js获取权限失败");
            }
        },
        error: function (err) {
            weui.topTips("微信js获取权限错误");
        }
    });
}

